/**
 * @param {number[][]} obstacleGrid
 * @return {number}
 */
var uniquePathsWithObstacles = function(obstacleGrid) {
    const m = obstacleGrid.length
    const n = obstacleGrid[0].length
    const dp = Array(m).fill().map(item => Array(n).fill(0))

    for (let i = 0; i < m && obstacleGrid[i][0] === 0; ++i) {
        dp[i][0] = 1
    }

    for (let i = 0; i < n && obstacleGrid[0][i] === 0; ++i) {
        dp[0][i] = 1
    }

    for (let i = 1; i < m; ++i) {
        for (let j = 1; j < n; ++j) {
            dp[i][j] = obstacleGrid[i][j] === 1 ? 0 : dp[i - 1][j] + dp[i][j - 1]
        }
    }

    return dp[m - 1][n - 1]
};



/**
 * @param {number[][]} obstacleGrid
 * @return {number}
 */
var uniquePathsWithObstacles = function(obstacleGrid) {
    if(obstacleGrid[0][0] === 1) return 0
    let l = obstacleGrid.length,r = obstacleGrid[0].length
    let flag = false
    for(let i = 0;i<l;i++){
        if(obstacleGrid[i][0] === 1 || flag){
            obstacleGrid[i][0] = 0
            flag = true
        }else{
            obstacleGrid[i][0] = 1
        }
    }
    flag = false
    for(let i = 1;i<r;i++){
        if(obstacleGrid[0][i] === 1 || flag){
            obstacleGrid[0][i] = 0
            flag = true
        }else{
            obstacleGrid[0][i] = 1
        }
    }
    for(let i = 1;i<l;i++){
        for(let j = 1;j<r;j++){
            obstacleGrid[i][j] = obstacleGrid[i][j] === 1 ? 0 : (obstacleGrid[i-1][j] + obstacleGrid[i][j-1])
        }
    }
    return obstacleGrid[l-1][r-1]
};